import Vue from 'vue'
import VueRouter from 'vue-router'
import store from "@/store";

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('../views/Register.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

const setRoutes = () => {
  const storeMenus = localStorage.getItem("menus");
  if(storeMenus){
    const manageRoute = {
      path: '/',
      component: () => import('../views/Manage.vue'),
      redirect: "/home",
      children: [

      ]
    }
  }
}

router.addRoute(  {
  path: '/',
  component: () => import('../views/Manage.vue'),
  redirect: "/home",
  // 注意这里是后台管理的子页面，不要放登录的路由
  children: [
    { path: 'home', name: '首页', component: () => import('../views/Home.vue')},
    { path: 'user', name: '用户管理', component: () => import('../views/User.vue')},
    { path: 'person', name: '个人信息', component: () => import('../views/Person.vue')},
    { path: 'file', name: '文件管理', component: () => import('../views/File.vue')},
    { path: 'role', name: '角色管理', component: () => import('../views/Role.vue')},
    { path: 'menu', name: '菜单管理', component: () => import('../views/Menu.vue')},
    { path: 'user/order', name: '用户订单管理', component: () => import('../views/UserOrder.vue')},
    { path: 'admin/order', name: '管理员订单管理', component: () => import('../views/AdminOrder.vue')},
    { path: 'admin/container', name: '管理员集装箱管理', component: () => import('../views/AdminContainer.vue')},
    { path: 'user/container', name: '用户集装箱管理', component: () => import('../views/UserContainer.vue')},
    { path: 'pickUp', name: '提箱订单分配', component: () => import('../views/PickUp.vue')},
    { path: 'yard', name: '堆场整体图', component: () => import('../views/Yard.vue')},
    { path: 'singleyard', name: '单一堆场图', component: () => import('../views/SingleYard.vue')},
  ]
},)

// 路由守卫
router.beforeEach((to, from, next) => {
  localStorage.setItem("currentPathName", to.name)  // 设置当前的路由名称，为了在Header组件中去使用
  store.commit("setPath")  // 触发store的数据更新
  next()  // 放行路由
})

export default router
